﻿/****************************************************
	文件：DotNetExtension.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/6/27 18:10:42
	功能：.net 扩展
*****************************************************/

#define HFormat_OPEN //开启字符串富文本

using System.Collections;
using System.Collections.Generic;
using Random = System.Random;

namespace HTFramework.Extension
{
    #region 枚举

    /// <summary>
    /// 字 颜色
    /// </summary>
    public enum E_FontColor
    {
        NoColor,

        /// <summary>
        /// 红色 #FF0000
        /// </summary>
        Red,

        /// <summary>
        /// 橙色 #FF7F00
        /// </summary>
        Orange,

        /// <summary>
        /// 黄色 #FFFE00
        /// </summary>
        Yellow,

        /// <summary>
        /// 绿色 #1BFF00
        /// </summary>
        Green,

        /// <summary>
        /// 青色 #00FFCD
        /// </summary>
        Cyan,

        /// <summary>
        /// 蓝色 #265FFF
        /// </summary>
        Blue,

        /// <summary>
        /// 紫色 #9E00FF
        /// </summary>
        Purple,

        /// <summary>
        /// 粉色 #FF2D8D
        /// </summary>
        Pink,

        /// <summary>
        /// 白色 #FFFFFF
        /// </summary>
        White,

        /// <summary>
        /// 灰色 #808080
        /// </summary>
        Gray,

        /// <summary>
        /// 黑色 #000000
        /// </summary>
        Black,
    }

    /// <summary>
    /// 字体
    /// </summary>
    public enum E_FontStyle
    {
        /// <summary>
        /// 普通
        /// </summary>
        Normal,

        /// <summary>
        /// 黑体
        /// </summary>
        Bold,
    }

    /// <summary>
    /// 字号 大小
    /// </summary>
    public enum E_FontSize
    {
        /// <summary>
        /// 小号 10
        /// </summary>
        Small,

        /// <summary>
        /// 中号 15
        /// </summary>
        Mid,

        /// <summary>
        /// 大号 20
        /// </summary>
        Large,
    }

    #endregion


    //------------------------------ Class ------------------------------//

    #region ClassExtension

    /// <summary>
    /// 类 扩展方法
    /// </summary>
    public static class ClassExtension
    {
        public static bool IsNull<T>(this T self) where T : class
        {
            return self == null;
        }

        public static bool IsNotNull<T>(this T self) where T : class
        {
            return self != null;
        }
    }

    #endregion


    //------------------------------ String ------------------------------//

    #region StringExtension

    /// <summary>
    /// 字符串 扩展方法
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// 格式化字符串（富文本）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="fontColor">颜色</param>
        /// <param name="fontStyle">字体</param>
        /// <param name="fontSize">字号</param>
        /// <returns></returns>
        public static string HFormat(this string str, E_FontColor fontColor = E_FontColor.NoColor,
            E_FontStyle fontStyle = E_FontStyle.Normal, E_FontSize fontSize = E_FontSize.Mid)
        {
#if UNITY_EDITOR
            #if HFormat_OPEN
            if (fontColor == E_FontColor.NoColor && fontStyle == E_FontStyle.Normal && fontSize == E_FontSize.Mid)
            {
                return str;
            }
            else
            {
                if (fontColor == E_FontColor.NoColor && fontStyle == E_FontStyle.Normal)
                {
                    return $"<size={GetSize(fontSize)}>{str}</size>";
                }
                else if (fontColor == E_FontColor.NoColor && fontSize == E_FontSize.Mid)
                {
                    return $"<b>{str}</b>";
                }
                else if (fontStyle == E_FontStyle.Normal && fontSize == E_FontSize.Mid)
                {
                    return $"<color={GetColorStr(fontColor)}>{str}</color>";
                }
                else
                {
                    if (fontColor == E_FontColor.NoColor)
                    {
                        return $"<b><size={GetSize(fontSize)}>{str}</size></b>";
                    }
                    else if (fontStyle == E_FontStyle.Normal)
                    {
                        return $"<size={GetSize(fontSize)}><color={GetColorStr(fontColor)}>{str}</color></size>";
                    }
                    else if (fontSize == E_FontSize.Mid)
                    {
                        return $"<b><color={GetColorStr(fontColor)}>{str}</color></b>";
                    }
                    else
                    {
                        return $"<b><size={GetSize(fontSize)}><color={GetColorStr(fontColor)}>{str}</color></size></b>";
                    }
                }
            }
            #else
            return str;
            #endif
#else
            return str;
#endif
        }


        static string GetColorStr(E_FontColor fontColor)
        {
            switch (fontColor)
            {
                case E_FontColor.NoColor:
                    return "";
                case E_FontColor.Red:
                    return "#FF0000";
                case E_FontColor.Orange:
                    return "#FF7F00";
                case E_FontColor.Yellow:
                    return "#FFFE00";
                case E_FontColor.Green:
                    return "#1BFF00";
                case E_FontColor.Cyan:
                    return "#00FFCD";
                case E_FontColor.Blue:
                    return "#265FFF";
                case E_FontColor.Purple:
                    return "#9E00FF";
                case E_FontColor.Pink:
                    return "#FF2D8D";
                case E_FontColor.White:
                    return "#FFFFFF";
                case E_FontColor.Gray:
                    return "#808080";
                case E_FontColor.Black:
                    return "#000000";
                default:
                    return "";
            }
        }

        static int GetSize(E_FontSize fontSize)
        {
            switch (fontSize)
            {
                case E_FontSize.Small:
                    return 10;
                case E_FontSize.Mid:
                    return 15;
                case E_FontSize.Large:
                    return 20;
                default:
                    return 15;
            }
        }
    }

    #endregion


    //------------------------------ ICollection ------------------------------//

    #region CollectionsExtension

    /// <summary>
    /// 集合 扩展方法
    /// </summary>
    public static class CollectionsExtension
    {
        public static bool IsNull(this ICollection array)
        {
            return array == null;
        }

        public static bool IsNullOrEmpty(this ICollection array)
        {
            if (array == null) return true;
            return array.Count == 0;
        }

        static Random mRand = new Random();

        /// <summary>
        /// 洗牌
        /// </summary>
        public static void Shuffle<T>(this IList<T> list)
        {
            int i = list.Count;
            while (i > 1)
            {
                i--;
                int index = mRand.Next(i + 1);
                T value = list[index];
                list[index] = list[i];
                list[i] = value;
            }
        }
    }

    #endregion
}